from flask_sqlalchemy import SQLAlchemy

db = SQLAlchemy()

class Comment(db.Model):
    __tablename__ = 'comment'

    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    pid = db.Column(db.Integer, default=0, nullable=False, comment='父级id')
    type = db.Column(db.Integer, nullable=False, default=1, comment='评论类型:1帖子')
    uid = db.Column(db.Integer, nullable=False, default=0, comment='评论作者ID')
    to_uid = db.Column(db.Integer, default=0, comment='被回复用户ID')
    voice_id = db.Column(db.Integer, comment='voice详情id')
    post_id = db.Column(db.Integer, nullable=False, default=0, comment='评论帖子ID')
    content = db.Column(db.String(255), nullable=False, default='', comment='评论内容')
    status = db.Column(db.SmallInteger, default=1, comment='评论状态')
    create_time = db.Column(db.DateTime, default=db.func.current_timestamp(), comment='创建时间')


    def to_dict(self):
        """Convert the model instance into a dictionary."""
        comment_data = {
            'id': self.id,
            'pid': self.pid,
            'type': self.type,
            'uid': self.uid,
            'toUid': self.to_uid,
            'voiceId': self.voice_id,
            'postId': self.post_id,
            'content': self.content,
            'status': self.status,
            'createTime': self.create_time.strftime('%Y-%m-%d %H:%M:%S') if self.create_time else None,
        }


        return comment_data
